package com.example.module_motocircle.motocirclesearch

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.activityViewModels
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.Router
import com.example.lib_common.base.BaseToolBarActivity
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.Success
import com.example.lib_common.observeWithLifecycle
import com.example.module_motocircle.R
import com.example.module_motocircle.databinding.ActivitySearchBinding
import com.example.module_motocircle.motocircle.MotocircleViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.FlowPreview

@AndroidEntryPoint
@Route(path = Router.Ui.MotocircleSearchActivity)
class MotocircleSearchActivity : BaseToolBarActivity<MotocircleViewModel,ActivitySearchBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_search
    private val mViewModel:MotocircleViewModel by viewModels()
    private val mSearchSuggestionsFragment by lazy {
        SearchSuggestionsFragment.newInstance(mOnlyTopic)
    }
    @Autowired(name = "search_type")
    lateinit var mSearchType: String
    private var mOnlyTopic = false
    @SuppressLint("CommitTransaction")
    override fun initView() {
        super.initView()
        showSoftKeyBoard(mBinding.etSearch)
        mOnlyTopic = mSearchType == "topic"
        supportFragmentManager.beginTransaction().add(R.id.l_container, mSearchSuggestionsFragment, SearchSuggestionsFragment.TAG)
            .hide(mSearchSuggestionsFragment)
            .commitAllowingStateLoss()
    }

    @OptIn(FlowPreview::class)
    override fun initData() {
        mBinding.viewModel=mViewModel
        mViewModel.getSearchKeywordList()
    }

    override fun initListener() {
        mViewModel.mKeywords.observeWithLifecycle(this){
            if (it.trim().isEmpty()){
                if (mSearchSuggestionsFragment.isVisible){
                    hideSuggestion()
                }
            }else{
                if (!mSearchSuggestionsFragment.isVisible){
                    showSuggestion()
                }
            }
        }
        mViewModel.mSearchSuggestionEvent.observeWithLifecycle(this){
            when(it){
                is Loading -> {
                    mSearchSuggestionsFragment.onLoading()
                }
                is Success -> {
                    mSearchSuggestionsFragment.onSuccess(mViewModel.mKeywords.value)
                    mSearchSuggestionsFragment.setNewData(mViewModel.mSuggestionList)
                }
                is Failed -> {
                    if (it.message == "暂无数据内容"){
                        mSearchSuggestionsFragment.clearData()
                    }
                    mSearchSuggestionsFragment.onError(it.message)
                }
                else ->{}
            }
        }
    }
    private fun showSuggestion(){
        supportFragmentManager.beginTransaction().show(mSearchSuggestionsFragment).commitAllowingStateLoss()
    }
    private fun hideSuggestion(){
        supportFragmentManager.beginTransaction().hide(mSearchSuggestionsFragment).commitAllowingStateLoss()
    }
}